package com.v512;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ServletContext sc;
	private String savePath;

	public void init(ServletConfig config) throws ServletException {
		savePath = config.getInitParameter("savePath");
		sc = config.getServletContext();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String uploader_name = (String) request.getParameter("uploadername");
		if (uploader_name == null || uploader_name.equals("")) {
			uploader_name = "上传人未知";
		}
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		try {
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> it = items.iterator();
			while (it.hasNext()) {
				FileItem item = (FileItem) it.next();
				if (item.isFormField()) {
					request.setAttribute("fieldName", item.getFieldName());
					request.setAttribute("parameterName", item.getString("UTF-8"));
//					System.out.println("表单的参数名称：" + item.getFieldName() + ",对应的参数值：" + item.getString("UTF-8"));
				} else {
					if (item.getName() != null && !item.getName().equals("")) {
						System.out.println("上传文件的大小：" + item.getSize() + "字节");
						System.out.println("上传文件的类型：" + item.getContentType());
						System.out.println("上传文件的名称：" + item.getName());
						System.out.println("上传人的名称：" + uploader_name);
						request.setAttribute("size", String.valueOf(item.getSize()));
						request.setAttribute("type", String.valueOf(item.getContentType()));
						request.setAttribute("name", String.valueOf(item.getName()));

						File file = new File(sc.getRealPath("/") + savePath, item.getName());
						item.write(file);
						
						request.setAttribute("uploader_name", uploader_name);
						request.setAttribute("upload.message", "上传文件成功！");
					} else {
						request.setAttribute("upload.message", "没有选择上传文件！");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("upload.message", "上传文件不成功！");
		}
		request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);
	}
	// TODO 实现文件下载：1、读取本地磁盘文件（非应用目录下）    2、应用在线生成文件并下载（文件输出给客户端）
}
